import sys
import json
import uuid
import boto3
from framework.common.pt_logger import logger
from framework.core.crawler_task import Task
from util import generalWrapper
s3_client = boto3.client('s3')
sqs_client = boto3.client('sqs')

# if sys.version_info[0] < 3:
#     from StringIO import StringIO # Python 2.x
# else:
#     from io import StringIO # Python 3.x

overridden_parameters = {
    "enableNotification": False
}
batch_id = "2021-05-03"
amazon_domain = "https://www.amazon.com"

def processRequest(req, event=None, context=None):
    if len(event["Records"]) != 1:
        raise Exception("only support single event in the given Records!")
    object_key = event["Records"][0]["s3"]["object"]["key"]
    if "part-" in object_key:
        bucket_name = event["Records"][0]["s3"]["bucket"]["name"]
        csv_obj = s3_client.get_object(Bucket=bucket_name, Key=object_key)
        body = csv_obj['Body']
        csv_string = body.read().decode('utf-8')
        logger.info("event: %s" % event)
        # logger.info("content: %s" % csv_string)
        base_queue_url = "https://sqs.us-east-2.amazonaws.com/599885534814/%s"
        dp_url_queue_url = base_queue_url % "pt-spider-serverless-AsinDetailPage-queue"
        qa_queue_url = base_queue_url % "pt-spider-serverless-AsinQuestionAndAnswer-queue"
        review_url_queue_url = base_queue_url % "pt-spider-serverless-AsinCustomerReview-queue"
        qa_task_list = list()
        dp_task_list = list()
        review_task_list = list()
        for line in csv_string.split("\n"):
            temp = line.split(",")
            asin_qa_url = amazon_domain + "/ask/questions/asin/%s/paging/ref=ask_ql_psf_ql_hza" % temp[0]
            asin_dp_url = amazon_domain + temp[1]
            asin_review_url = amazon_domain + temp[2]
            qa_task_list.append(composeTask(batch_id, asin_qa_url, "AsinQuestionAndAnswer"))
            dp_task_list.append(composeTask(batch_id, asin_dp_url, "AsinDetailPage"))
            review_task_list.append(composeTask(batch_id, asin_review_url, "AsinCustomerReview"))
            break

        sendToQueue(qa_queue_url, qa_task_list)
        sendToQueue(dp_url_queue_url, dp_task_list)
        sendToQueue(review_url_queue_url, review_task_list)


def sendToQueue(queue_url, tasks):
    uuids = [str(uuid.uuid1()) for _ in tasks]
    tasks_dict_list = [t.__dict__ for t in tasks]
    entries = list()
    for i, t in enumerate(tasks_dict_list):
        task_obj = {
            "Id": uuids[i],
            "MessageBody": json.dumps(tasks_dict_list[i], indent=4)
        }
        entries.append(task_obj)
    # SQS允许最大10个消息作为entries入队列，这里每10个分一批进行batch enqueue操作
    entries_batches = [entries[x:x + 10] for x in range(0, len(entries), 10)]
    for entries_batch in entries_batches:
        response = sqs_client.send_message_batch(
            QueueUrl=queue_url,
            Entries=entries_batch
        )
        if response["ResponseMetadata"]["HTTPStatusCode"] == 200:
            logger.info("enqueue tasks, %s tasks is queuing to %s" % (len(entries_batch), queue_url))
        else:
            logger.warning("send message failed...")


def composeTask(batch_id, url, spider_name):
    spiderMapping = {
        "AsinDetailPage": {
            "parser": "dpParser"
        },
        "AsinCustomerReview": {
            "parser": "reviewParser"
        },
        "AsinQuestionAndAnswer": {
            "parser": "qaParser"
        }
    }
    body = {
        "url": url,
        "metadata": {
            "batchId": batch_id,
            "tt": spider_name,
        },
        "parser": spiderMapping[spider_name]["parser"],
        "desc": None,
        "tags": None,
        "parent_result": None
    }
    return Task.from_json(body)


globals()['generalWrapper'].global_parameters = {**globals()['generalWrapper'].global_parameters, **overridden_parameters}
globals()['generalWrapper'].processRequest = processRequest
globals()['handler'] = generalWrapper.handler

"""
{
    "Records": [
        {
            "eventVersion": "2.1",
            "eventSource": "aws:s3",
            "awsRegion": "us-east-2",
            "eventTime": "2021-05-20T07:43:18.645Z",
            "eventName": "ObjectCreated:Put",
            "userIdentity": {
                "principalId": "AWS:AIDAJGXTP6SVZPME7AQXI"
            },
            "requestParameters": {
                "sourceIPAddress": "140.207.23.117"
            },
            "responseElements": {
                "x-amz-request-id": "A27Z70F2JHHP2B22",
                "x-amz-id-2": "vKADnSkulkGK6X6oG3LsJEkuYSQ4OyW/+/DRNOEa74mTiKmb33qzAr5Wi7Hpf5vAxc1PTslLR1BDcz79nBq9LOgm5QZGMo96"
            },
            "s3": {
                "s3SchemaVersion": "1.0",
                "configurationId": "f6735774-1e10-4789-a636-d26cfdf39e6d",
                "bucket": {
                    "name": "pt-ss-s3-mr-preprocessasinoutputbucket-1su2gvlqiuuqe",
                    "ownerIdentity": {
                        "principalId": "A261VVT7JB02YM"
                    },
                    "arn": "arn:aws:s3:::pt-ss-s3-mr-preprocessasinoutputbucket-1su2gvlqiuuqe"
                },
                "object": {
                    "key": "part-00000-3de7a2d7-a69d-4946-8d6d-de8cb1d714b1-c000.csv",
                    "size": 8141,
                    "eTag": "aab59d84c8ac8e88c5046d58b3b19eee",
                    "sequencer": "0060A613242424ECFA"
                }
            }
        }
    ]
}
"""